/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.yoyo.fragment.reading;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.core.BaseFragment;
import com.xuexiang.yoyo.databinding.FragmentReadingTestBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

/**
 * @author liuxiangfei
 * @since 2023-07-24 00:19
 */
@Page(anim = CoreAnim.none)
public class ReadingTestFragment extends BaseFragment<FragmentReadingTestBinding> {


    @NonNull
    @Override
    protected FragmentReadingTestBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentReadingTestBinding.inflate(inflater, container, attachToRoot);
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle().setTitle("压力测试");
        titleBar.addAction(
                new TitleBar.TextAction("设置"){

                    @Override
                    public void performAction(View view) {
                        XToastUtils.toast("点击测试");
                    }
                }
        );
        return titleBar;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        initPressTestNum();
        initGroupListView();
    }


    XUICommonListItemView itemWithSwitch;
    private void initGroupListView() {
        itemWithSwitch = binding.groupListView.createItemView("开启压力测试");
        itemWithSwitch.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        itemWithSwitch.getSwitch().setOnCheckedChangeListener((buttonView, isChecked) ->{
            HesAppStatic.setPressTestFlag(isChecked);
            binding.etPressTestNum.setEnabled(isChecked);
        });

        int size = DensityUtils.dp2px(getContext(), 20);
        XUIGroupListView.newSection(getContext())
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(itemWithSwitch, onClickListener)
                .addTo(binding.groupListView);

        itemWithSwitch.getSwitch().setChecked(HesAppStatic.isPressTestFlag());
    }
    View.OnClickListener onClickListener = v -> {
        if (v instanceof XUICommonListItemView) {
            CharSequence text = ((XUICommonListItemView) v).getText();
            XToastUtils.toast(text + " is Clicked");
        }
    };

    private void initPressTestNum() {

        binding.etPressTestNum.setText(HesAppStatic.getPressTestNum()+"");
        binding.etPressTestNum.setOnFocusChangeListener((v, hasFocus) -> {
            if(!hasFocus){
                HesAppStatic.setPressTestNum(binding.etPressTestNum.getText().toString());
                XToastUtils.toast(binding.etPressTestNum.getText());
            }
        });


    }
}
